package com.k.rabbitmq.amqp.demo;

import com.k.rabbitmq.RabbitMqConst;
import com.rabbitmq.client.*;

import java.nio.charset.StandardCharsets;

public class RabbitMqListener {
    private static void rabbitListener(String vhost, String host, Integer port, String username, String password, String exchangeName, String queueName, String[] bindingKeys) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);
        factory.setUsername(username);
        factory.setPassword(password);
        factory.setPort(port);
        factory.setVirtualHost(vhost);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        // 判断 Exchange 是否存在
        channel.exchangeDeclarePassive(exchangeName);
        // 声明 Exchange 已经存在不需要声明
        channel.exchangeDeclare(exchangeName, "fanout",false,false,null);
        // 声明 Queue
        channel.queueDeclare(queueName, true, false, true, null);
        if (bindingKeys != null && bindingKeys.length > 0) {
            for (String bindingKey : bindingKeys) {
                channel.queueBind(queueName, exchangeName, bindingKey);
            }
        } else {
            channel.queueBind(queueName, exchangeName, "");
        }
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println(" [x] Received '" + delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
        };
        // https://www.cnblogs.com/piaolingzxh/p/5448927.html
        // autoAck：是否自动ack，如果不自动ack，需要使用channel.ack、channel.nack、channel.basicReject 进行消息应答
        channel.basicConsume(RabbitMqConst.QueueName.TEST_QUEUE_101, true, deliverCallback, consumerTag -> {
        });
        System.out.println("Consumer finish");
    }

    public static void fanoutListener(String vhost, String host, Integer port, String username, String password, String exchangeName, String queueName) throws Exception {
        rabbitListener(vhost, host, port, username, password, exchangeName, queueName, null);
    }

    private static void topicListener(String vhost, String host, Integer port, String username, String password, String exchangeName, String queueName, String[] routingKey) throws Exception {
        rabbitListener(vhost, host, port, username, password, exchangeName, queueName, routingKey);
    }

    public static void main(String[] args) {
        try {
            fanoutListener("mcc_vhost",
                    "10.110.12.180",
                    27000,
                    "admin",
                    "Command_123",
                    RabbitMqConst.ExchangeName.ALARM_TYPE,
                    RabbitMqConst.QueueName.TEST_QUEUE_101);
            topicListener("mcc_vhost",
                    "10.110.12.180",
                    27000,
                    "admin",
                    "Command_123",
                    RabbitMqConst.ExchangeName.ALARM_TYPE,
                    RabbitMqConst.QueueName.TEST_QUEUE_101,
                    new String[]{
                            "DATA.CHANGE.GROUP",
                            "DATA.CHANGE.DEVICE"});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
